﻿using Hr.IDAL;
using Hr.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Versioning;
using System.Text;
using System.Threading.Tasks;

namespace Hr.DAL
{
    public partial class UserDAL : BaseDAL<User_List>, IUser_ListDAL
    {
        // 创建EF对象
        private DbContext db = DbContextFactory.Create();


        /// <summary>
        /// 根据ID获取用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public User_List GetUserInfo(int id)
        {
            return db.Set<User_List>().SingleOrDefault(n => n.pk_User == id);
        }

        /// <summary>
        /// 根据ID获取职位信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public User_Position GetPositionInfo(int id)
        {
            return db.Set<User_Position>().SingleOrDefault(n => n.pk_Position == id);
        }
        
        /// <summary>
        /// 根据ID获取部门信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public User_Department GetDepartmentInfo(int id)
        {
            return db.Set<User_Department>().SingleOrDefault(n => n.pk_Department == id);
        }

        /// <summary>
        /// 获取部门Ltst信息
        /// </summary>
        /// <param name="DepartmentName"></param>
        /// <returns></returns>
        public List<User_Department> GetUserDepartment(string DepartmentName)
        {
            if (DepartmentName == null)
            {
                DepartmentName = "";
            }
            return (
                from Department in db.Set<User_Department>()
                where Department.DepartmentName.Contains(DepartmentName)
                select Department
                ).ToList();

            #region 单表条件查询，例子
            // return db.Set<User_List>().Where(n => n.pk_User == id);
            #endregion
        }

        /// <summary>
        /// 获取职位
        /// </summary>
        /// <returns></returns>
        public List<User_Position> GetUserPosition(string Title)
        {
            if (Title == null)
            {
                Title = "";
            }
            return (from Position in db.Set<User_Position>()
                    where Position.Title.Contains(Title)
                    select Position
                    ).OrderBy(n => n.LevelID).ToList();
        }

        /// <summary>
        /// 获取用户管理,模糊查询
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public IEnumerable<object> GetUserList(string DName,string RName,string FName,string UName,string Phone,string NumberID)
        {
            DName = DName == null ? "" : DName;
            RName = RName == null ? "" : RName;
            FName = FName == null ? "" : FName;
            UName = UName == null ? "" : UName;
            Phone = Phone == null ? "" : Phone;
            NumberID = NumberID == null ? "" : NumberID;
            return (from UserList in db.Set<User_List>()
                   join Department in db.Set<User_Department>() on UserList.fk_Department equals Department.pk_Department
                    join Position in db.Set<User_Position>() on UserList.Position equals Position.pk_Position
                    join roleName in db.Set<Role_Name>() on UserList.RoleValue equals roleName.pk_Name
                    orderby UserList.pk_User
                    where Department.DepartmentName.ToString().Contains(DName)
                    & roleName.RoleName_CHS.ToString().Contains(RName)
                    & UserList.FullName.ToString().Contains(FName)
                    & UserList.UserName.ToString().Contains(UName)
                    & UserList.Phone1.ToString().Contains(Phone)
                    & UserList.Phone2.ToString().Contains(Phone)
                    & UserList.IDNumber.ToString().Contains(NumberID)
                    select new
                    {
                        UserList.pk_User,
                        UserList.UserName,
                        Department.DepartmentName,
                        UserList.FullName,
                        UserList.Wages,
                        UserList.Phone1,
                        UserList.IsLock,
                        UserList.ImagePath1,
                        roleName.RoleName_CHS
                    }
                ).ToList();
        }

        /// <summary>
        /// 获取在线用户信息
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public IEnumerable<object> GetOnLineList(string Name)
        {
            if (Name==null)
            {
                Name = "";
            }
            return (
                 from OnLine in db.Set<User_Online>()
                 join User in db.Set<User_List>() on OnLine.fk_User equals User.pk_User
                 join Department in db.Set<User_Department>() on User.fk_Department equals Department.pk_Department
                 join Position in db.Set<User_Position>() on User.Position equals Position.pk_Position
                 where User.FullName.Contains(Name)&OnLine.LoginTime.ToString().Equals(db.Set<User_Online>().OrderBy(s => s.LoginTime).FirstOrDefault(ol => ol.fk_User== User.pk_User).LoginTime.ToString())
                 select new
                 {
                     User.ImagePath1,
                     User.FullName,
                     User.UserName,
                     Department.DepartmentName,
                     Position.Title,
                     User.Phone1,
                     User.Remark,
                     User.LastLoginTime,
                     OnLine.LoginTime
                 }
                ).OrderBy(n => n.LoginTime);
        }

        //登录 查询用户表中是否有该用户
        public User_List GetUser(string UserName, string Password)
        {
            return db.Set<User_List>().SingleOrDefault(n => n.UserName == UserName && n.Password == Password);
        }

        /// <summary>
        /// 获取类型
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public IQueryable<T> GetMyModels<T>() where T:class
        {
            return db.Set<T>();
        } 

        /// <summary>
        /// 添加，修改用户管理
        /// </summary>
        /// <param name="u"></param>
        /// <param name="type"></param>
        public void UpdateUserList(User_List u,string type,int pk_User)
        {
            if (type.Equals("Add"))
            {
                u.Password = "123456";
                u.TypeID = 0;
                u.RoleList = u.RoleValue.ToString();
                u.CreateDate = DateTime.Now;
                u.CreateUser = pk_User;
                u.IsLock = false;//是否禁用
                db.Set<User_List>().Add(u);
            }else if (type.Equals("Edit"))
            {
                u.ModifyDate = DateTime.Now;
                u.ModifyUser = pk_User;
                db.Set<User_List>().AddOrUpdate(u);
            }
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="l"></param>
        public void DeleteUserList(User_List l)
        {
            db.Set<User_List>().Remove(l) ;
        }

        /// <summary>
        /// 添加，修改职位管理
        /// </summary>
        /// <param name="u"></param>
        /// <param name="type"></param>
        public void UpdatePosition(User_Position p, string type,int pk_User)
        {
            
            if (type.Equals("Add"))
            {
                p.CreateDate = DateTime.Now;
                p.CreateUser = pk_User;
                p.ParentID = 0;
                p.fk_Company = 0;
                p.SortID = 0; 
                db.Set<User_Position>().Add(p);
            }
            else if (type.Equals("Edit"))
            {
                p.ModifyDate = DateTime.Now;
                p.ModifyUser = pk_User;
                db.Set<User_Position>().AddOrUpdate(p);
            }
        }
        /// <summary>
        /// 删除职业
        /// </summary>
        /// <param name="p"></param>
        public void DeletePosition(User_Position p)
        {
            db.Set<User_Position>().Remove(p);
        }

        /// <summary>
        /// 添加，修改部门管理
        /// </summary>
        /// <param name="d"></param>
        /// <param name="type"></param>
        public void UpdateDepartment(User_Department d, string type,int pk_User)
        {
            if (type.Equals("Add"))
            {
                d.Tuesday = d.Monday;
                d.Wednesday = d.Monday;
                d.Thursday = d.Monday;
                d.Friday = d.Monday;
                d.CreateDate = DateTime.Now;
                db.Set<User_Department>().Add(d);
            }
            else if (type.Equals("Edit"))
            {
                //User_Department ud = db.Set<User_Department>().SingleOrDefault(n => n.pk_Department == d.pk_Department);
                d.ModifyDate = DateTime.Now;
                d.ModifyUser = pk_User;
                db.Set<User_Department>().AddOrUpdate(d);
            }
        }

        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="d"></param>
        public void DeleteDepaerment(User_Department d)
        {
            //Userlist
            List<User_List> UL = db.Set<User_List>().Where(n => n.fk_Department == d.pk_Department).ToList();

            //Address_List
            List<Address_List> AL = db.Set<Address_List>().Where(n => n.fk_Department == d.pk_Department).ToList();

            //
            List<Attendance_List> AL2 = db.Set<Attendance_List>().Where(n => n.fk_Department == d.pk_Department).ToList();
            List<Calendar_List> CL = db.Set<Calendar_List>().Where(n => n.fk_Department == d.pk_Department).ToList();
            List<Car_List> CL1 = db.Set<Car_List>().Where(n => n.fk_Department == d.pk_Department).ToList();
            List<Customer_Follow> CL2= db.Set<Customer_Follow>().Where(n => n.fk_Department == d.pk_Department).ToList();
            List<Customer_List> CL3 = db.Set<Customer_List>().Where(n => n.fk_Department == d.pk_Department).ToList();
            List<Finance_Account> FA = db.Set<Finance_Account>().Where(n => n.fk_Department == d.pk_Department).ToList();
            List<Finance_List> FA2 = db.Set<Finance_List>().Where(n => n.fk_Department == d.pk_Department).ToList();
            List<Mail_Account> MA = db.Set<Mail_Account>().Where(n => n.fk_Department == d.pk_Department).ToList();
            List<Meeting_List> ML = db.Set<Meeting_List>().Where(n => n.fk_Department == d.pk_Department).ToList();
            List<Notice_List> NL = db.Set<Notice_List>().Where(n => n.fk_Department == d.pk_Department).ToList();
            List<Notice_Receiver> NR = db.Set<Notice_Receiver>().Where(n => n.fk_Department == d.pk_Department).ToList();

            //删除
            db.Set<Attendance_List>().RemoveRange(AL2);
            db.Set<User_List>().RemoveRange(UL);
            db.Set<Address_List>().RemoveRange(AL);
            db.Set<Calendar_List>().RemoveRange(CL);
            db.Set<Car_List>().RemoveRange(CL1);
            db.Set<Customer_Follow>().RemoveRange(CL2);
            db.Set<Customer_List>().RemoveRange(CL3);
            db.Set<Finance_Account>().RemoveRange(FA);
            db.Set<Finance_List>().RemoveRange(FA2);
            db.Set<Mail_Account>().RemoveRange(MA);
            db.Set<Meeting_List>().RemoveRange(ML);
            db.Set<Notice_List>().RemoveRange(NL);
            db.Set<Notice_Receiver>().RemoveRange(NR);
           

            db.Set<User_Department>().Remove(d);
        }

        //实现接口
        public bool SaveChanges()
        {
            return db.SaveChanges() > 0;
        }
    }
}
